package ch05.patterns;

import ch05.patterns.VecMathLexer;
import ch05.patterns.VecMathParser;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

import java.io.IOException;

/**
 * 改写、化简数乘向量
 * 将输入 4 * [0, 5*0, 3] 的树化简为 [4*0, 4*5*0, 4*3]
 */

public class Test {
    public static void main(String[] args) throws IOException, RecognitionException {
        CharStream input = null;
        if (args.length > 0) {
            input = new ANTLRStringStream(args[0]);
        } else {
            input = new ANTLRInputStream(System.in);
        }

        // create lexer/parser to build trees from stdin
        VecMathLexer lexer = new VecMathLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        VecMathParser p = new VecMathParser(tokens);
        RuleReturnScope r = p.prog();
        CommonTree t = (CommonTree)r.getTree();

        System.out.println("Original tree: " + t.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        Simplify s = new Simplify(nodes);
        t = (CommonTree)s.downup(t, true);
        System.out.println("Simplified tree: " + t.toStringTree());
    }
}
